﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.OleDb;
using System.Data;

/// <summary>
///News 的摘要说明
/// </summary>
public class News
{
	public News()
	{
		//
		//TODO: 在此处添加构造函数逻辑
		//
	}



    public OleDbDataAdapter GetNewsAll()
    {
        
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbDataAdapter odp = new OleDbDataAdapter("select NID,Ntitle,Ntime,HtmlUrl from [News] Order BY NID DESC", myconn);
        return odp;
    }
    public OleDbDataReader GetNewsAllDR()
    {

        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("select * from [News] Order By NID DESC ", myconn);
        OleDbDataReader dr = null;
        try
        {
            myconn.Open();
            dr = mycmd.ExecuteReader(CommandBehavior.CloseConnection);
        }

        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        return dr;
    }
    public int GetNewsCount()
    {
        int s = 0;
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("select Count(*) from [News] ", myconn);

        try
        {
            myconn.Open();
            s = int.Parse(mycmd.ExecuteScalar().ToString());
        }

        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myconn.Close();
        }

        return s;
    }
    public OleDbDataReader GetNewsConn(int NID)
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("select * from [News] where NID=" + NID + " ", myconn);
        OleDbDataReader dr = null;
        try
        {
            myconn.Open();
            dr = mycmd.ExecuteReader(CommandBehavior.CloseConnection);
        }

        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }

        return dr;
    }
    public int ADDNews(string Ntitle, string Ntitle2, string Nkey, string description, string Ncon)
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("Insert into News(Ntitle,Ntitle2,Nkey,description,Ncon) Values ('" + Ntitle + "','" + Ntitle2 + "','" + Nkey + "','" + description + "',@Ncon)", myconn);
        mycmd.Parameters.AddWithValue("@Ncon", Ncon);
        int s = 0;
        try
        {
            myconn.Open();
            s = mycmd.ExecuteNonQuery();

        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        myconn.Close();
        return s;
    }
    public int ADDNewsHtmlUrl(string Ntitle, string Ntitle2, string Nkey, string description, string Ncon,string htmlurl)
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("Insert into News(Ntitle,Ntitle2,Nkey,description,Ncon,HtmlUrl) Values ('" + Ntitle + "','" + Ntitle2 + "','" + Nkey + "','" + description + "',@Ncon,'" + htmlurl + "')", myconn);
        mycmd.Parameters.AddWithValue("@Ncon", Ncon);
        int s = 0;
        try
        {
            myconn.Open();
            s = mycmd.ExecuteNonQuery();

        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        myconn.Close();
        return s;
    }
    public int UPPNews(string Ntitle, string Ntitle2, string Nkey, string description, string Ncon, int NID)
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("UPdate [News] Set Ntitle=@Ntitle,Ntitle2=@Ntitle2,Nkey=@Nkey,description=@description,Ncon=@Ncon where NID="+NID+"", myconn);
        mycmd.Parameters.AddWithValue("@Ntitle", Ntitle);
        mycmd.Parameters.AddWithValue("@Ntitle2", Ntitle2);
        mycmd.Parameters.AddWithValue("@Nkey", Nkey);
        mycmd.Parameters.AddWithValue("@description", description);
        mycmd.Parameters.AddWithValue("@Ncon", Ncon);
        int s = 0;
        try
        {
            myconn.Open();
            s = mycmd.ExecuteNonQuery();

        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        myconn.Close();
        return s;
    }
    public int UPPNewsHtmlUrl(int NID)
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("UPdate [News] set HtmlUrl='S" + NID.ToString() + ".html' where NID=" + NID + "", myconn);

        int s = 0;
        try
        {
            myconn.Open();
            s = mycmd.ExecuteNonQuery();

        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        myconn.Close();
        return s;
    }
    public DataSet GetNewsPage(int SPage1, int SPage2, string Sname)
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("select * from [News] Order By NID DESC", myconn);
        OleDbDataAdapter dr = new OleDbDataAdapter(mycmd);
        DataSet da = new DataSet();
        try
        {
            myconn.Open();
            dr.Fill(da, SPage1, SPage2, Sname);
        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myconn.Close();
        }
        return da;
    }
    //删除数据
    public int DelNews(int sId)
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("delete from [News] where NID = @NID ", myconn);
        mycmd.Parameters.AddWithValue("@NID", sId);
        int nResult = -1;
        try
        {
            myconn.Open();
            nResult = mycmd.ExecuteNonQuery();
        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myconn.Close();
        }
        return nResult;
    }


    public int GetNewsTop1NID()
    {
        OleDbConnection myconn = new OleDbConnection(DB.conn);
        OleDbCommand mycmd = new OleDbCommand("Select Top 1 NID from [News] Order BY NID DESC ", myconn);
        int nResult = -1;
        try
        {
            myconn.Open();
            nResult = int.Parse(mycmd.ExecuteScalar().ToString());
        }
        catch (OleDbException ex)
        {
            throw new Exception(ex.Message, ex);
        }
        finally
        {
            myconn.Close();
        }
        return nResult;
    }
}